package com.wonderful.eyepetizer.ui.pages.discover

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Place
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material.icons.filled.ThumbUp
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.collectAsLazyPagingItems
import coil.compose.rememberAsyncImagePainter
import com.wonderful.eyepetizer.app.ktx.loadMoreView
import com.wonderful.eyepetizer.data.entity.bean.RecommendItem
import com.wonderful.eyepetizer.ui.theme.Black_87
import com.wonderful.eyepetizer.utils.ScreenUtils

/**
 * Desc:
 * @Author: wonderful
 * @Time: 2024/2/23 14:32
 */
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun RecommendPage(viewModel: DiscoverViewModel = viewModel()) {
    val lazyPagingItems = viewModel.recommendFlow.collectAsLazyPagingItems()
    val pullRefreshState = rememberPullRefreshState(
        refreshing = viewModel.recommendRefreshing.value,
        onRefresh = { lazyPagingItems.refresh() })

    Box(
        modifier = Modifier
            .fillMaxSize()
            .pullRefresh(pullRefreshState)
    ) {
        LazyVerticalStaggeredGrid(
            columns = StaggeredGridCells.Fixed(2),
            contentPadding = PaddingValues(5.dp),
            verticalItemSpacing = 10.dp,
            content = {
                items(lazyPagingItems.itemCount) {
                    lazyPagingItems[it]?.let { recommendItem ->
                        RecommendItemWidget(recommendItem = recommendItem)
                    }
                }
                loadMoreView(lazyPagingItems)
            })

        PullRefreshIndicator(
            refreshing = viewModel.recommendRefreshing.value,
            state = pullRefreshState,
            modifier = Modifier.align(Alignment.TopCenter)
        )
    }
}

@Composable
fun RecommendItemWidget(recommendItem: RecommendItem) {
    Card(
        shape = RoundedCornerShape(5.dp),
        backgroundColor = Color.White,
        modifier = Modifier.padding(5.dp)
    ) {
        val maxWidth = ScreenUtils.getScreenWidth()
        var width = maxWidth
        recommendItem.data.content.data?.width?.let {
            if (it != 0) width = it
        }
        var height = maxWidth
        recommendItem.data.content.data?.height?.let {
            if (it != 0) height = it
        }
        val collectionCount = remember {
            mutableIntStateOf(recommendItem.data.content.data?.consumption?.collectionCount ?: 0)
        }

        Column {
            Box(modifier = Modifier.fillMaxWidth()) {
                Image(
                    painter = rememberAsyncImagePainter(model = recommendItem.data.content.data?.cover?.feed),
                    contentDescription = null,
                    modifier = Modifier
                        .fillMaxWidth()
                        .aspectRatio(width / height.toFloat()),
                    contentScale = ContentScale.FillWidth
                )
                recommendItem.data.content.data?.urls?.let {
                    if (it.isNotEmpty()) {
                        Icon(
                            imageVector = if (recommendItem.data.content.type == "video") Icons.Default.PlayArrow else Icons.Default.Place,
                            contentDescription = null,
                            tint = Color.White,
                            modifier = Modifier
                                .size(18.dp)
                                .padding(end = 5.dp, top = 5.dp)
                                .align(Alignment.TopEnd)
                        )
                    }
                }
            }
            Text(
                text = recommendItem.data.content.data?.description ?: "",
                style = TextStyle(fontSize = 14.sp, color = Black_87),
                maxLines = 2,
                overflow = TextOverflow.Ellipsis,
                modifier = Modifier.padding(horizontal = 6.dp, vertical = 10.dp)
            )
            Row(
                modifier = Modifier.padding(horizontal = 6.dp, vertical = 10.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Image(
                    painter = rememberAsyncImagePainter(model = recommendItem.data.content.data?.owner?.avatar),
                    contentDescription = null,
                    modifier = Modifier
                        .size(24.dp)
                        .clip(CircleShape)
                )
                Text(
                    text = recommendItem.data.content.data?.owner?.nickname ?: "",
                    fontSize = 12.sp,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis,
                    modifier = Modifier
                        .width(80.dp)
                        .padding(3.dp)
                )
                Spacer(modifier = Modifier.weight(1f))
                Icon(
                    imageVector = Icons.Default.ThumbUp,
                    contentDescription = null,
                    modifier = Modifier
                        .size(14.dp)
                        .clickable {
                            collectionCount.intValue++
                        },
                    tint = Color.Gray
                )
                Text(
                    text = "${collectionCount.intValue}",
                    fontSize = 12.sp,
                    modifier = Modifier.padding(start = 3.dp)
                )
            }
        }
    }
}